package com.jinzihan.web;

import com.jinzihan.entity.Student;
import com.jinzihan.service.StudentService;
import com.jinzihan.service.impl.StudentServiceImpl;
import com.jinzihan.utils.WebUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class StudentServlet extends BaseServlet {
    protected void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        StudentService studentService = new StudentServiceImpl();
        Student student = WebUtils.copyParamToBean(req.getParameterMap(), new Student());
        int id = studentService.queryUserId(student.getUsername());
        student.setStudentId(id);
        Student loginStudent = studentService.login(student);
        if (loginStudent == null) {
            req.setAttribute("msg","用户名或密码错误");
            String username = req.getParameter("username");
            req.setAttribute("username",username);
            req.getRequestDispatcher("/pages/user/login.jsp").forward(req,resp);
        } else {
            System.out.println("登陆成功");
            req.getSession().setAttribute("student", student);
            req.getRequestDispatcher("/pages/user/login_success.jsp").forward(req, resp);
        }
    }

    protected void logout(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.getSession().invalidate();
        resp.sendRedirect(req.getContextPath());
    }
}